package com.leonzx.base.controller.webview;

import com.leonzx.base.controller.base.BaseController;
import com.leonzx.base.entity.system.User;
import com.leonzx.base.service.RedisService;
import com.leonzx.base.service.im.ImService;
import com.leonzx.base.utils.RedisConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.net.UnknownHostException;
import java.util.Map;

/**
 * @Author:李晓雷
 * @Description:
 * @Date: 18/07/09 14:43
 */
@Controller
@RequestMapping("/webview/layim")
public class WebviewLayimController extends BaseController {
    @Value("${jim.serviceIp}")
    private String serviceIp;

    @Value("${jim.port}")
    public Integer port;

    @Autowired
    private ImService imService;
    @Autowired
    private RedisService redisService;
    /**
     * 静态模版所在路径前缀
     */
    private String prefix = "webview/layim";
    /**
     * 登陆聊天系统
     *
     *
     * @param token 登录凭证
     * @param model
     * @return
     * @throws UnknownHostException
     */
    @RequestMapping("/login")
    public String login(String token,  Model model) throws UnknownHostException {
        model.addAttribute("serviceIp", serviceIp);
        model.addAttribute("port", port);
        String userJson = redisService.getValue(RedisConstants.USER_TOKEN + token);
        //
        User user = gson.fromJson(userJson, User.class);
        //获得用户初始化数据
        Map<String, Object> data = imService.getLayImInitData(user);
        model.addAttribute("userId", user.getId());
        model.addAttribute("name", user.getName());
        model.addAttribute("userName", user.getUsername());
        model.addAttribute("initData", gson.toJson(data));
        return prefix + "/chat-root";
    }
}